package com.example.dingtalkwebproject;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

import java.util.Collections;

@SpringBootApplication
public class DingTalkWebProjectApplication extends SpringBootServletInitializer {


    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(DingTalkWebProjectApplication.class);
    }

    @Bean
    public RestTemplate restTemplate() {
        RestTemplate template = new RestTemplate();

        // 添加必须的请求头
        template.setInterceptors(Collections.singletonList((request, body, execution) -> {
            request.getHeaders().add("Content-Type",  "application/json");
            request.getHeaders().add("x-acs-dingtalk-api-gateway-sign",  "v3");
            return execution.execute(request,  body);
        }));

        return template;
    }

    public static void main(String[] args) {
        SpringApplication.run(DingTalkWebProjectApplication.class, args);
    }

}
